﻿/**
 * @des 检测数字字符串是否为两位，不足两位补齐
 * @param str 要检测的字符串
 */
function getFull(str) {
	if (parseInt(str) == str) {
		if (str < 10) {
			return "0" + str;
		} else {
			return str;
		}
	}
}


/**
 * @des 获取当前时间
 */
function getTime() {
	var now:Date = new Date();
	
	return getFull(now.getHours()) + ":" + getFull(now.getMinutes()) + ":" + getFull(now.getSeconds());
}


/**
 * @des 将十四位数字的时间串转换成日期格式
 * @param str 要转换的时间串
 */
function swapTime(str) {
	var tmp:String = "";
	
	if (str.length < 12) {
		return false;
	} else {
		tmp = str.substr(0, 4) + "-" + str.substr(4, 2) + "-" + str.substr(6, 2) + " " + str.substr(8, 2) + ":" + str.substr(10, 2);
	}
	return tmp;
}


/**
 * @des 弹出系统提示框
 * @param t 标题
 * @param c 内容
 * @param flag 按钮个数 1/2
 */
function alert(t, c, flag) {
	with (mx.controls.Alert) {
		okLabel = "确定";
		cancelLabel = "取消";
		switch (parseInt(flag)) {
			case 1:
				show(c, t, OK, _root, null, "alertIcon");
				break;
			case 2:
				show(c, t, OK|CANCEL, _root, null, "alertIcon");
				break;
		}
	}
}


/**
 * @des 初始化环境变量
 */
function init() {
	if (outIp == undefined) {
		// 调试模式
		serverIp = "127.0.0.1";
		memberId = "省中心_administrator";
		memberName = "省中心_Administrator";
		memberIdList = "省中心_administrator,省中心_test,省中心_Kampiner,厦门中心_administrator,厦门中心_xm";
		memberNameList = "省中心_administrator,省中心_测试者,省中心_Kampiner,厦门中心_Admin,厦门中心_Xm";
	} else {
		// 发布模式
		src = outSrc;
		serverIp = outIp;
		memberId = outMemberId;
		memberName = outMemberName;
		importOutData();
	}
	
	this.createTextField("mn", this.getNextHighestDepth(), 0, 5, 215, 20);
	mn.html = true;
	mn.autoSize = "right";
	mn.htmlText = "<b><font color='#071E81'>" + memberName + "</font> </b>";
}


/**
 * @des 导入外部数据
 *      如成员列表，由于成员列表中数据很多，用GET方式来直接获取会丢失数据
 */
function importOutData() {
	var lv:LoadVars = new LoadVars();
	lv.sendAndLoad(src, lv, "POST");
	
	lv.onData = function(src:String) {
		/**
		 * @des 导入的数据为成员ID列表与成员名称列表，用$拆分
		 */
		
		if (src.indexOf("$") == -1) {
			nc.call("upLog", null, getTime() + "import out data is field...");
		} else {
			memberIdList = src.split("$")[0];
			memberNameList = src.split("$")[1];
			
			sp.contentPath = "uList";
			setTimeout(syncState, 1000);
		}
	};
}


/**
 * @des 用户在线同步
 *      先查看是否IM在线，然后再查看是否已经参加会议，会议状态下是不可以用聊天工具进行聊天的．
 */
function syncState() {
	var so:SharedObject = SharedObject.getRemote("im", nc.uri);
	so.connect(nc);
	
	so.onSync = function() {
		var memberIdListArray = memberIdList.split(",");
		var memberNameListArray = memberNameList.split(",");
		
		//修正状态
		var flag;
		for (var i:Number = 0; i < memberIdListArray.length; i++) {
			flag = false;
			if (eval("sp.content.un" + i).state == "在线" && memberIdListArray[i] != memberId) {
				for (var j in so.data) {
					if (memberIdListArray[i] == so.data[j].memberId) {
						flag = true;
					}
				}
				if (!flag) {
					var o = eval("sp.content.stat" + i);
					var x1 = o._x;
					var y1 = o._y;
					
					removeMovieClip(o);
					sp.content.attachMovie("offline",
										   "stat" + i,
										   sp.content.getNextHighestDepth(),
										   {
											   _x: x1,
											   _y: y1
										   });
					eval("sp.content.un" + i).state = "离线";
				}
			}
		}
		
		// 遍历所有的用户列表
		for (var i:Number = 0; i < memberIdListArray.length; i++) {
			// 遍历所有的IM在线用户列表
			for (var j in so.data) {
				if (memberIdListArray[i] == so.data[j].memberId && memberIdListArray[i] != memberId && so.data[j].memberId != memberId) {
					var o = eval("sp.content.stat" + i);
					var x1 = o._x;
					var y1 = o._y;
					
					removeMovieClip(o);
					sp.content.attachMovie("online",
										   "stat" + i,
										   sp.content.getNextHighestDepth(),
										   {
											   _x: x1,
											   _y: y1
										   });
					eval("sp.content.un" + i).state = "在线";
				}
			}
		}
		
		// 同步会议室中的成员状态
		var mso:SharedObject = SharedObject.getRemote("meeting", nc.uri, false);
		mso.connect(nc);
		
		mso.onSync = function() {
			var memberIdListArray = memberIdList.split(",");
			var memberNameListArray = memberNameList.split(",");
			
			
			//修正状态
			var flag;
			for (var i:Number = 0; i < memberIdListArray.length; i++) {
				flag = false;
				if (eval("sp.content.un" + i).state == "忙碌" && memberIdListArray[i] != memberId) {
					for (var j in mso.data) {
						if (memberIdListArray[i] == mso.data[j].memberId) {
							flag = true;
						}
					}
					if (!flag) {
						var o = eval("sp.content.stat" + i);
						var x1 = o._x;
						var y1 = o._y;
						
						removeMovieClip(o);
						sp.content.attachMovie("online",
											   "stat" + i,
											   sp.content.getNextHighestDepth(),
											   {
												   _x: x1,
												   _y: y1
											   });
						eval("sp.content.un" + i).state = "在线";
					}
				}
			}

			for (var i:Number = 0; i < memberIdListArray.length; i++) {
				for (var j in mso.data) {
					if (memberIdListArray[i] == mso.data[j].memberId && memberIdListArray[i] != memberId && mso.data[j].memberId != memberId) {
						var o = eval("sp.content.stat" + i);
						var x1 = o._x;
						var y1 = o._y;
					
						removeMovieClip(o);
						sp.content.attachMovie("busy",
											   "stat" + i,
											   sp.content.getNextHighestDepth(),
											   {
												   _x: x1,
												   _y: y1
											   });
						eval("sp.content.un" + i).state = "忙碌";
					}
				}
			}
		};
	};
}